import java.util.Arrays;

public class myArrayList {
    public int[] elem;
    public int usedSize;//有效的数据个数,默认为0

    public myArrayList() {
        this.elem = new int[10];//给数组默认10个长度
    }

    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < size(); i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    // 获取顺序表的有效数据长度
    public int size() {
        return this.usedSize;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //考虑数组长度和下标合法性
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
        }
        if (pos < 0 || pos > size()){
            return;
        }
        for (int i = this.usedSize - 1; i >= pos; i--) {//这里的usedSize要注意
            this.elem[i + 1] = this.elem[i];
        }
        //数据别忘了放进去，放一个加一次
        this.elem[pos] = data;
        this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]){
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]){
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        //别忘了判断pos的合法性和数组是否为空
        if (pos < 0 || pos > this.usedSize){
            System.out.println("pos值非法");
            return -1;
        }
        if (this.usedSize == 0){
            System.out.println("数组为空");
            return -1;
        }
        return this.elem[pos];
    }

    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (pos < 0 || pos > this.usedSize){
            System.out.println("pos值非法");
            return;
        }
        if (this.usedSize == 0){
            System.out.println("数组为空");
            return;
        }
        this.elem[pos] = value;
    }

    //删除数字toRemove
    public void remove(int toRemove) {
        if (toRemove < 0 || toRemove > this.usedSize){
            System.out.println("pos值非法");
            return;
        }
        if (this.usedSize == 0){
            System.out.println("数组为空");
            return;
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (toRemove == this.elem[i]){
                for (int j = i; j < this.usedSize - 1; j++) {
                    this.elem[j] = this.elem[j + 1];
                }
            }
        }
        this.usedSize--;
    }

    //就地逆置顺序表
    public void reverse(){
        for (int i = 0; i < usedSize / 2; i++) {
            int tmp = elem[i];
            elem[i] = elem[usedSize - 1 - i];
            elem[usedSize - 1 - i] = tmp;
        }
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }

}
